#include "Entity3d.h"
#include "Node.h"
#include <d3dx9.h>

using namespace ns;

Entity3d::Entity3d()
:
localTransform( new D3DXMATRIX() ),
wordTransform( new D3DXMATRIX() ),
position(new D3DXMATRIX()),
rotation (new D3DXMATRIX()),
scale (new D3DXMATRIX()),
parent(NULL),
posX(0), posY(0), posZ(0),rotX(0),rotY(0),rotZ(0),scaleX(1),scaleY(1),scaleZ(1)
{
	UpdateLocalTransformation();
}

Entity3d::~Entity3d()
{
	delete localTransform;
	localTransform = NULL;

	delete wordTransform;
	wordTransform = NULL;
}

void Entity3d::UpdateTransformation()
{	
	if (parent)
	{	
		D3DXMatrixMultiply(wordTransform,parent->wordTransform,localTransform);
	}
	else
	{
		wordTransform = localTransform;
	}
}


float Entity3d::GetPosX(){return posX;}
float Entity3d::GetPosY(){return posY;}
float Entity3d::GetPosZ(){return posZ;}

float Entity3d::GetRotX(){return rotX;}
float Entity3d::GetRotY(){return rotY;}
float Entity3d::GetRotZ(){return rotZ;}

float Entity3d::GetScaleX(){return scaleX;}
float Entity3d::GetScaleY(){return scaleY;}
float Entity3d::GetScaleZ(){return scaleZ;}

void Entity3d::SetPosX(float x)
{
	posX=x; 
	UpdateLocalTransformation();
}
void Entity3d::SetPosY(float y)
{
	posY=y;
	UpdateLocalTransformation();
}
void Entity3d::SetPosZ(float z)
{
	posZ=z;
	UpdateLocalTransformation();
}
void Entity3d::SetRotX(float x){
	rotX=x;
	UpdateLocalTransformation();
}
void Entity3d::SetRotY(float y){
	rotY=y;
	UpdateLocalTransformation();
}
void Entity3d::SetRotZ(float z){
	rotZ=z;
	UpdateLocalTransformation();
}
void Entity3d::SetScaleX(float x){
	scaleX=x;
	UpdateLocalTransformation();
}
void Entity3d::SetScaleY(float y){
	scaleY=y;
	UpdateLocalTransformation();
}
void Entity3d::SetScaleZ(float z){
	scaleZ=z;
	UpdateLocalTransformation();
}


void Entity3d::SetParent(Node* node){parent = node;}
Node* Entity3d::GetParent(){return parent;}

void Entity3d::UpdateLocalTransformation()
{
	D3DXMatrixTranslation(position, posX, posY, posZ);
	D3DXMatrixScaling(scale,scaleX,scaleY,scaleZ);
	D3DXMatrixMultiply(localTransform,position,scale);
	D3DXMatrixRotationYawPitchRoll(rotation,rotX,rotY,rotZ);
	D3DXMatrixMultiply(localTransform,localTransform,rotation);
}